define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/mrp/index' + location.search,
                    add_url: 'scanwork/mrp/add',
                    del_url: 'scanwork/mrp/del',
                    table: 'scanwork_mrp_plan',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'plan_no', title: '计划编号'},
                        {field: 'plan_name', title: '计划名称'},
                        {field: 'start_date', title: '开始日期', formatter: Table.api.formatter.date},
                        {field: 'end_date', title: '结束日期', formatter: Table.api.formatter.date},
                        {field: 'run_status', title: '运算状态', searchList: {0:'待运算',1:'运算中',2:'已完成',3:'失败'}, formatter: function(value) {
                            var statusMap = {0:'<span class="label label-default">待运算</span>',1:'<span class="label label-info">运算中</span>',2:'<span class="label label-success">已完成</span>',3:'<span class="label label-danger">失败</span>'};
                            return statusMap[value] || '';
                        }},
                        {field: 'run_time', title: '运算时间', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'run',
                                    text: '执行运算',
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: 'fa fa-calculator',
                                    url: 'scanwork/mrp/run',
                                    confirm: '确认执行MRP运算吗？',
                                    success: function() {
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

